home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / MAP Viewer / Camera.cpp < prev    next >
C/C++ Source or Header  |  2003-10-09  |  3KB  |  119 lines

  1. /*
  2. Half-Life MAP viewing utility.
  3. Copyright (C) 2003  Ryan Samuel Gregg
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include "stdafx.h"
  21. #include "Camera.h"
  22.  
  23. CCamera::CCamera()
  24. {
  25.     Reset();
  26.  
  27.     vUp.X = 0.0f;
  28.     vUp.Y = 0.0f;
  29.     vUp.Z = 1.0f;
  30.  
  31.     fTimeElapsed = 0.0f;
  32.     lLastTick = 0;
  33.  
  34.     fFPS = 0.0f;
  35.     lLastFPSUpdateTick = 0;
  36.     lFrames = 0;
  37.     fFrameInterval = 500.0f;
  38.  
  39.     bInvertCamera = false;
  40. }
  41.  
  42. void CCamera::InitializePerformanceCounter()
  43. {
  44.     fTimeElapsed = 0.0f;
  45.     fFPS = 0.0f;
  46. //    QueryPerformanceCounter(&lLastTick);
  47.     lLastTick = GetTickCount();
  48.     lLastFPSUpdateTick = lLastTick;
  49. //    QueryPerformanceFrequency(&lFreq);
  50. }
  51.  
  52. void CCamera::UpdateTimeElapsed()
  53. {
  54.     long lTick;
  55.     float fFPSUpdateTimeElapsed;
  56.     //QueryPerformanceCounter(&lTick);
  57.     lTick = GetTickCount();
  58.  
  59.     fTimeElapsed = (float)(lTick - lLastTick);// / (float)(lFreq);
  60.     fFPSUpdateTimeElapsed = (float)(lTick - lLastFPSUpdateTick);// / (float)(lFreq);
  61.     if(fFPSUpdateTimeElapsed >= fFrameInterval) 
  62.     {    
  63.         fFPS = 1000.0f * (float)(lFrames) / (fFPSUpdateTimeElapsed);
  64.         lLastFPSUpdateTick = lTick;
  65.         lFrames = 0;
  66.     }
  67.  
  68.     lFrames++;
  69.     lLastTick = lTick;
  70. }
  71.  
  72. void CCamera::MoveCameraAngles(int iAngleX, int iAngleY)
  73. {
  74.     if(bInvertCamera)
  75.         iAngleX = -iAngleX;
  76.  
  77.     vAngle.X -= (float)(iAngleX) / 100.0f;
  78.     vAngle.Y += (float)(iAngleY) / 100.0f;
  79.  
  80.     if(vAngle.X > 1.5f)  // 85.94 degrees.
  81.         vAngle.X = 1.5f;
  82.  
  83.     if(vAngle.X < -1.5f)
  84.         vAngle.X = -1.5f;
  85. }
  86.  
  87. void CCamera::MoveCamera(float fWalk, float fStrafe)
  88. {
  89.     vPosition.X += ((float)(Math::Cos(vAngle.Y)) * fWalk * fTimeElapsed) + ((float)(Math::Cos(vAngle.Y + 1.570796f)) * fStrafe * fTimeElapsed);
  90.     vPosition.Y += ((float)(Math::Sin(vAngle.Y)) * fWalk * fTimeElapsed) + ((float)(Math::Sin(vAngle.Y + 1.570796f)) * fStrafe * fTimeElapsed);
  91.     vPosition.Z -= ((float)(Math::Tan(vAngle.X)) * fWalk * fTimeElapsed);
  92. }
  93.  
  94. void CCamera::CalculateLookAt()
  95. {
  96.     vLookAt.X = vPosition.X + (float)(Math::Cos(vAngle.Y));
  97.     vLookAt.Y = vPosition.Y + (float)(Math::Sin(vAngle.Y));
  98.     vLookAt.Z = vPosition.Z - (float)(Math::Tan(vAngle.X));
  99. }
  100.  
  101. void CCamera::SetCamera()
  102. {
  103.     CalculateLookAt();
  104.     gluLookAt(vPosition.X, vPosition.Y, vPosition.Z, vLookAt.X, vLookAt.Y, vLookAt.Z, vUp.X, vUp.Y, vUp.Z);
  105. }
  106.  
  107. void CCamera::Reset()
  108. {
  109.     vPosition.X = 0.0f;
  110.     vPosition.Y = 0.0f;
  111.     vPosition.Z = 0.0f;
  112.  
  113.     vLookAt.X = 1.0f;
  114.     vLookAt.Y = 0.0f;
  115.     vLookAt.Z = 0.0f;
  116.  
  117.     vAngle.X = 0.0f;
  118.     vAngle.Y = 0.0f;
  119. }